<template>
  <div id="home"
       class="wrapper">
    <nav-bar class="home-nav">
      <div slot="center">高品商城</div>
    </nav-bar>
    <!-- 狸猫换太子  -->
    <!-- 导航控制 -->
    <tab-control :titles="titles"
                 class="tab-control"
                 @tabclick='tabClick'
                 ref="tabOffsetTop1"
                 v-show="isTabShow"></tab-control>
    <!-- 设置better-scroll组件 添加滚动 -->
    <scroll class="content"
            ref="wrapper"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <!-- 轮播 -->
      <home-swiper :banners='banners'
                   @swiperImageLoad="swiperImageLoad"></home-swiper>
      <!-- 推荐商品-->
      <home-recommend :recommends="recommends"></home-recommend>
      <!-- 本周流行 -->
      <home-feature></home-feature>
      <!-- 导航控制 -->
      <tab-control :titles="['流行','新款','精选']"
                   class="tab-control"
                   @tabclick='tabClick'
                   ref="tabOffsetTop2"
                   v-show="!isTabShow"></tab-control>
      <!-- 商品列表 -->
      <!-- 动态获取currentType -->
      <home-goods-list :goods="showGoods"></home-goods-list>
    </scroll>
    <!-- 返回顶部 -->
    <back-top @click.native="backClick"
              v-show="isShowBacktop"></back-top>

  </div>
</template>

<script>
// 公共组件
import NavBar from 'components/common/navbar/navBar'
// 导入homeswiper组件
import homeSwiper from './childComps/HomeSwiper'
//导入HomeRecommends组件
import HomeRecommend from './childComps/HomeRecommend'
// 本周排行
import HomeFeature from './childComps/Feature'
// 导入导航控制组件
import TabControl from 'components/content/tabControl/TabControl'
//导入导航切换goodslist组件
import HomeGoodsList from 'components/content/goodsList/goodsList'
//导入bettter-scroll 公共组件
import Scroll from 'components/common/scroll/Scroll'
// 导入返回顶部组件
import BackTop from 'components/common/BackTop/BackTop'
//引入network中的首页请求js
import { getHomeMultidata, getHomeGoodsData } from 'network/home'
//引入工具类
import { debounce } from '@/utilsCommon/utils'

export default {
  name: "home",
  components: {
    NavBar,
    homeSwiper,
    HomeRecommend,
    HomeFeature,
    TabControl,
    HomeGoodsList,
    Scroll,
    BackTop
  },
  data () {
    return {
      banners: [],
      recommends: [],
      // 导航文字
      titles: ['流行', '新款', '精选'],
      //导航控制切换数据
      goods: {
        "pop": { page: 0, list: [] },
        "new": { page: 0, list: [] },
        "sell": { page: 0, list: [] },
      },
      //设置当前的流行、精选、精品的参数为pop
      currentType: 'pop',
      // 返回顶部按钮
      isShowBacktop: false,
      // 吸顶效果
      tabOffsetTop: 0,
      isTabShow: false


    }
  },
  computed: {
    showGoods () {
      return this.goods[this.currentType].list
    }

  },
  created () {
    // 1、请求多个数据
    this.getHomeMultidata()
    // // 2、请求商品数据pop news sell
    this.getHomeGoodsData('pop')
    this.getHomeGoodsData('new')
    this.getHomeGoodsData('sell')

  },
  mounted () {
    //防抖
    const refresh = debounce(this.$refs.wrapper.refresh, 50)
    // 1、监听item中图片加载完成
    this.$bus.$on('itemImageLoad', () => {
      refresh()
    })

    //2、获取tabOffsetTop的offsettop
    //所有组件都有一个属性$el:用于获取组件中的元素
    // console.log(this.$refs.tabOffsetTop.$el.offsetTop);


  },
  destoryed () {
    console.log('不要销毁');
  },
  activated () {
    //保存位置
    this.$refs.wrapper.scrollTo(0, this.saveY, 0)

    this.$refs.wrapper.refresh()
  },
  deactivated () {
    this.saveY = this.$refs.wrapper.getScrollY()
  },
  methods: {
    /* 
    事件监听相关的方法
     */
    tabClick (index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break;
        case 2:
          this.currentType = 'sell'
          break;
      }
      this.$refs.tabOffsetTop1.currentIndex = index;
      this.$refs.tabOffsetTop2.currentIndex = index;
    },
    // 监听组件点击
    backClick () {
      console.log('1231');
      this.$refs.wrapper.scrollTo(0, 0)
    },
    contentScroll (position) {
      // 1、判断backTop是否显示
      this.isShowBacktop = (-position.y) > 1000
      //2、决定tabControl是否吸顶
      this.isTabShow = (-position.y) > this.tabOffsetTop - 44
    },
    // 上拉加载更多
    loadMore () {
      this.getHomeGoodsData(this.currentType)
      // this.getHomeGoodsData(this.currentType)
    },
    //监听swiperImage加载完成
    swiperImageLoad () {
      this.tabOffsetTop = this.$refs.tabOffsetTop2.$el.offsetTop
    },
    /*
    网络请求的相关方法
    */
    getHomeMultidata () {
      getHomeMultidata().then(res => {
        // console.log(res);
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list
      })
    },
    // 请求商品切换数据
    getHomeGoodsData (type) {
      //page
      const page = this.goods[type].page + 1
      getHomeGoodsData(type, page).then(res => {
        // console.log(res);
        //把数据push到对象里
        this.goods[type].list.push(...res.data.list)
        //插入数据 原来的page加1 即0+1
        this.goods[type].page += 1
        // 调用fishPullUp方法
        this.$refs.wrapper.finishPullUp()
      })
    }

  }
}

</script>
<style scoped>
#home {
  /**
  * 有了BS之后，可以不需要这2个 
  * padding-top: 44px;
  * padding-bottom: 49px;
  * 下方BS的.wrapper样式里就有 top44 bottom49
  /* 一定要有home的高度，否则其子对象wrapper无法正确显示 */
  height: 100vh;
  /* margin-top: 44px; */
}
/* 在组件nav-bar上设置class 统一改变背景色 */
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9; */
}
/* 由于用了better-scroll已经失效 */
/* 为了在使用原生滚动时，导航栏不随着内容滚动 */
/* .tab-control {
  position: sticky;
  top: 44px;
} */
.tab-control {
  position: relative;
  z-index: 9;
}
/* 滚动插件样式 */
.content {
  overflow: hidden;
  height: calc(100vh - 93px);
}
</style>